package com.student.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.entity.ExamScore;
import com.student.education.dao.ExamScoreDao;
import com.student.education.service.ExamScoreService;
import com.student.education.service.UserService;
import com.student.education.to.ExamScoreQueryTo;
import com.student.education.to.ExamScoreTo;
import com.student.education.to.UserTo;
import com.student.education.vo.ExamScoreQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 考试成绩表(ExamScore)表服务实现类
 *
 * @author makejava
 * @since 2021-10-04 09:45:14
 */
@Service("examScoreService")
public class ExamScoreServiceImpl implements ExamScoreService {
    @Resource
    private ExamScoreDao examScoreDao;
    @Autowired
    UserService userService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ExamScore queryById(Integer id) {
        return this.examScoreDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ExamScore> queryAllByLimit(int offset, int limit) {
        return this.examScoreDao.queryAllByLimit(offset, limit);
    }

    /**
     * 修改数据
     *
     * @param examScore 实例对象
     * @return 实例对象
     */
    @Override
    public ExamScore update(ExamScore examScore) {
        this.examScoreDao.update(examScore);
        return this.queryById(examScore.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.examScoreDao.deleteById(id) > 0;
    }

    @Override
    public int insert(List<ExamScoreTo> list) {
        return examScoreDao.insert(list);
    }

    @Override
    public CommonPage<ExamScoreQueryTo> queryMyStudent(ExamScoreQueryVo vo) {
        UserTo userTo = userService.queryById(vo.getUserId());
        //学生
        if (Objects.equals(userTo.getRoleId(), "3")) {
            vo.setStudentId(userTo.getId());
        }else if (Objects.equals(userTo.getRoleId(), "4")&&vo.getClassId()==null){
            vo.setClassId(userTo.getClassIdList());
        }
        if (vo.getPageUtil() != null) {
            PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
        }
        List<ExamScoreQueryTo> list = examScoreDao.queryMyStudentScore(vo);
        return CommonPage.restPage(list);
    }

}